package com.zoran.study.spring.model;


import lombok.ToString;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.Serializable;

/**
 * Student
 *
 * @author zouxm
 * @date 16/12/7
 */
@ToString
public class Author implements Serializable, BeanNameAware,
        InitializingBean, ApplicationContextAware, DisposableBean {


    private static final long serialVersionUID = 7794014142666168432L;

    private String name;

    private Book book;

    public Author() {
        System.out.println("【构造器】实例化...");

    }

    public void setName(String name) {
        System.out.println("【注入属性】name");
        this.name = name;
    }

    public void setBook(Book book) {
        System.out.println("【注入属性】book");
        this.book = book;
    }

    public void init() {
//        this.name="hello-good";
        System.out.println("【init-method】");
    }

    @PostConstruct
    public void postConstruct2() {
        System.out.println("【Post-Construct2】");
    }

    @PostConstruct
    public void postConstruct1() {
        System.out.println("【Post-Construct1】");
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("【BeanNameAware接口】调用BeanNameAware.setBeanName()--->" + name);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("【InitializingBean接口】调用InitializingBean.afterPropertiesSet() ");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("【ApplicationContextAware接口】");
        System.out.println(applicationContext);
        System.out.println("===================");
    }

//    如果Bean的scope是prototype 则不会调用销毁相关的方法

    @Override
    public void destroy() throws Exception {
        System.out.println("【DisposableBean接口】DisposableBean.destroy()");
    }

    @PreDestroy
    public void preDestroy() {
        System.out.println("【PreDestroy annotation】");
    }

}
